import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class UserProfilePanel extends JPanel{

	private JTextField txtUsername;
	private JPasswordField pwdPassword;
	private UserProfile userProfile;
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	
	public UserProfilePanel(){
		this.setPreferredSize(new Dimension(400,100));
		
		txtUsername = new JTextField(10);
		pwdPassword = new JPasswordField(10);
		
		JButton btnLogin = new JButton("Login");
		JLabel lblUsername = new JLabel("Username:");
		JLabel lblPassword = new JLabel("Password:");
		
		this.add(lblUsername);
		this.add(txtUsername);
		this.add(lblPassword);
		this.add(pwdPassword);
		this.add(btnLogin);
		
		LoginHandler btnLoginHandler = new LoginHandler();
		btnLogin.addActionListener(btnLoginHandler);
	}
	
	private class LoginHandler implements ActionListener{
		public void actionPerformed(ActionEvent event){
			try {
				userProfile = new UserProfile(txtUsername.getText(),String.valueOf(pwdPassword.getPassword()));
				updateProfilePanel();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
	}
	
	private void updateProfilePanel() throws Exception{
		this.removeAll();
		//JLabel lblDspName = new JLabel(userProfile.getDisplayName());
		this.add(new JLabel(userProfile.getDisplayName()));
		this.add(new JLabel("Karma:"+userProfile.getKarma()));
		this.add(new JLabel("Unread:"+userProfile.getUnreadCount()));
		this.updateUI();
		
	}
}
